package iit.projects.etaxi.view.bean;

import java.util.List;

import javax.faces.event.ActionEvent;

import com.icesoft.faces.component.ext.HtmlInputText;

import iit.projects.etaxi.model.dto.CustomerSCDO;
import iit.projects.etaxi.model.entity.Customer;
import iit.projects.etaxi.model.service.CustomerService;
import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.JSFUtils;

public class ManageCustomersBackingBean {
	
	private Customer customer;
	private List<Customer> customers;
	private String selectedCustomerIdForDelete;
	private String selectedCustomerIdForSuspend;
	
	private HtmlInputText compFName;
	private HtmlInputText compMN;
	private HtmlInputText compCCode;
	
	private CustomerService customerService;
	private ServiceLocator serviceLocator;
	
	@javax.annotation.PostConstruct
	public void init(){
		customerService = serviceLocator.getCustomerService();
		this.customer = new Customer();
	}
	
	public void searchCustomers(ActionEvent event){
		
		CustomerSCDO customerSCDO = new CustomerSCDO();
		customerSCDO.setFirstName(this.customer.getFirstName());
		customerSCDO.setMobileNumber(this.customer.getMobileNumber());
		customerSCDO.setCustomerCode(this.customer.getCustomerCode());
		
		this.customers = customerService.findCustomers(customerSCDO);
		if(this.customers == null || this.customers.isEmpty()){
			JSFUtils.addFacesInfoMessage("data.notfound");
			return;
		}
		//clear(event);
	}
	
	public void confirmDelete(ActionEvent event){
		ConfirmationPopupBackingBean confirmBean = 
			(ConfirmationPopupBackingBean)JSFUtils.getManagedBeanValue("confirmationPopup");
		confirmBean.setMessage(JSFUtils.getResourcesProperty("delete.customer.confirmation"));
		confirmBean.setBackingBeanAlias("manageCustomerBean");
		confirmBean.setMethodName("deleteCustomer");
		confirmBean.setShowPopup(true);
		
		selectedCustomerIdForDelete = ((String)JSFUtils.getRequestParameter("code"));
	}
	
	public void deleteCustomer(ActionEvent event){
		
		for(Customer customer : this.customers){
			if(customer.getCustomerCode().equals(selectedCustomerIdForDelete)){
				
				try{
					customerService.deleteCustomer(customer);
					this.customers.remove(customer);
				}catch(Throwable e){
					return;
				}
				break;
			}
		}
	}
	
	public void confirmSuspend(ActionEvent event){
		ConfirmationPopupBackingBean confirmBean = 
			(ConfirmationPopupBackingBean)JSFUtils.getManagedBeanValue("confirmationPopup");
		confirmBean.setMessage(JSFUtils.getResourcesProperty("suspend.customer.confirmation"));
		confirmBean.setBackingBeanAlias("manageCustomerBean");
		confirmBean.setMethodName("suspendCustomer");
		confirmBean.setShowPopup(true);
		
		selectedCustomerIdForSuspend = ((String)JSFUtils.getRequestParameter("code"));
	}
	
	public void suspendCustomer(ActionEvent event){
		
		for(Customer customer : this.customers){
			if(customer.getCustomerCode().equals(selectedCustomerIdForSuspend)){
				
				try{
					customer.setSuspended(true);
					customerService.updateCustomer(customer);
				}catch(Throwable e){
					return;
				}
				break;
			}
		}
	}
	
	public void confirmClear(ActionEvent event){
		ConfirmationPopupBackingBean confirmBean = 
			(ConfirmationPopupBackingBean)JSFUtils.getManagedBeanValue("confirmationPopup");
		confirmBean.setMessage(JSFUtils.getResourcesProperty("clear.suspension.customer.confirmation"));
		confirmBean.setBackingBeanAlias("manageCustomerBean");
		confirmBean.setMethodName("clearSuspension");
		confirmBean.setShowPopup(true);
		
		selectedCustomerIdForSuspend = ((String)JSFUtils.getRequestParameter("code"));
	}
	
	public void clearSuspension(ActionEvent event){
		
		for(Customer customer : this.customers){
			if(customer.getCustomerCode().equals(selectedCustomerIdForSuspend)){
				
				try{
					customer.setSuspended(false);
					customerService.updateCustomer(customer);
				}catch(Throwable e){
					return;
				}
				break;
			}
		}
	}
	
	public void clear(ActionEvent event){
		this.customer = new Customer();
		compFName.resetValue();
		compMN.resetValue();
		compCCode.resetValue();
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public HtmlInputText getCompFName() {
		return compFName;
	}

	public void setCompFName(HtmlInputText compFName) {
		this.compFName = compFName;
	}

	public HtmlInputText getCompCCode() {
		return compCCode;
	}

	public void setCompCCode(HtmlInputText compCCode) {
		this.compCCode = compCCode;
	}

	public HtmlInputText getCompMN() {
		return compMN;
	}

	public void setCompMN(HtmlInputText compMN) {
		this.compMN = compMN;
	}
	

}
